﻿using System.Data.Objects;

namespace Sern.Common
{
    internal static class RepositoryHelper
    {
        /// <summary>
        /// Check whether an object is attached to an object set
        /// </summary>
        public static bool IsAttached<T>(this ObjectSet<T> objectSet, T entity) where T : class
        {
            ObjectStateEntry entry = null;
            if (objectSet.Context.ObjectStateManager.TryGetObjectStateEntry(entity, out entry))
            {
                return entry.State != System.Data.EntityState.Detached;
            }
            return false;
        }
    }
}
